package ubadb.core.components.bufferManager.bufferPool.replacementStrategies.fifo;

import java.util.Comparator;

import ubadb.core.common.Page;
import ubadb.core.components.bufferManager.bufferPool.BufferFrame;
import ubadb.core.components.bufferManager.bufferPool.replacementStrategies.AbstractPageReplacementStrategy;

/**
 * FIFO replacement strategy with behavior equivalent to FIFOReplacementStrategy
 * but with implementation based on AbstractPageReplacementStrategy.
 */
public class FIFO2ReplacementStrategy extends AbstractPageReplacementStrategy
{
	
	public FIFO2ReplacementStrategy() {
		super(
			new Comparator<BufferFrame>()
			{
				@Override
				public int compare(BufferFrame bf1, BufferFrame bf2) {
					// safe casts as we know all frames are of this type
					FIFOBufferFrame fbf1 = (FIFOBufferFrame) bf1; 
					FIFOBufferFrame fbf2 = (FIFOBufferFrame) bf2;
					
					return fbf2.getCreationDate().compareTo(fbf1.getCreationDate());
				}
			}
		);
	}
	
	public BufferFrame createNewFrame(Page page)
	{
		return new FIFOBufferFrame(page);
	}
}
